package com.net.advancedThread;

import java.util.concurrent.Semaphore;

public class SemaphoreTest 
{

	 Semaphore semaphore = new Semaphore(1);
	
	public static void main(String[] args) 
	{
		
		final SemaphoreTest test = new SemaphoreTest();
		
		new Thread(){
			public void run()
			{
				test.mutualExclusion();
			}
			
		}.start();
		
		
		new Thread(){
			public void run()
			{
				test.mutualExclusion();
			}
			
		}.start();;

	}

	protected void mutualExclusion()
	{
		try {
			
			semaphore.acquire();
			
			System.out.println( Thread.currentThread().getName()+ "   aquired lock ");
			
			Thread.sleep(1000);
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		finally
		{
			semaphore.release();
			
			System.out.println(Thread.currentThread().getName() + "   released lock ");
		}
		
	}

}
